﻿Imports TyranntDungeonDatabase
Imports TyranntUserDatabase

Public Class DungeonRoutines

    Private udb As New UserDatabaseAccess
    Private ddb As New DungeonDatabaseAccess
    Private _userID As Integer

    Private Const AdminUser As Integer = 1
    Private Const NormalUser As Integer = 2

    Public Function AddDungeonObject(ByVal token As String, ByVal dungeonObjXML As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            If IsAdmin() = True Then
                Return ddb.NewMazeObject(dungeonObjXML)
            Else
                Return ErrorDataUser(-4, "AddDungeonObject")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function UpdateDungeonObject(ByVal token As String, ByVal dungeonObjXML As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            If IsAdmin() = True Then
                Return ddb.UpdateMazeObject(dungeonObjXML)
            Else
                Return ErrorDataUser(-4, "UpdateDungeonObject")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function DeleteDungeonObject(ByVal token As String, ByVal dungeonObjName As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            If IsAdmin() = True Then
                Return ddb.DeleteMazeObject(dungeonObjName)
            Else
                Return ErrorDataUser(-4, "DeleteDungeonObject")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GetDungeonObject(ByVal token As String, ByVal dungeonObjName As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            If IsAdmin() = True Then
                Return ddb.GetMazeObject(dungeonObjName)
            Else
                Return ErrorDataUser(-4, "GetDungeonObject")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

    Public Function GetDungeonObjects(ByVal token As String) As String
        GetUserID(token)
        If _userID >= 0 Then
            If IsAdmin() = True Then
                Return ddb.GetMazeObjects()
            Else
                Return ErrorDataUser(-4, "GetDungeonObjects")
            End If
        Else
            Return ErrorDataUser(_userID, "GetUserID")
        End If
    End Function

#Region "General Routines"

    Private Function IsAdmin() As Boolean
        Dim userGroup As Integer = udb.GetUserGroup(_userID)
        If userGroup = AdminUser Then
            Return True
        Else
            Return False
        End If
    End Function

    Private Sub GetUserID(ByVal token As String)
        _userID = udb.GetUserIDFromToken(token)
    End Sub

    Private Function ErrorDataUser(ByVal code As Integer, ByVal routineName As String) As String
        Select Case code
            Case -1
                Return "[#]usernotexist[" + routineName + "]"
            Case -2
                Return "[#]failed[" + routineName + "] - " + udb.LastError.ToString
            Case Else
                Return "[#]unknowncode[" + code.ToString + "],[" + routineName + "]"
        End Select
    End Function

    Private Function ErrorDataDungeon(ByVal code As Integer, ByVal routineName As String) As String
        Select Case code
            Case -1
                Return "[#]usernotexist[" + routineName + "]"
            Case -2
                Return "[#]failed[" + routineName + "] - " + ddb.LastError.ToString
            Case -3
                Return "[#]permissiondenied[" + routineName + "]"
            Case -4
                Return "[#]characternotbelongtoyou[" + routineName + "]"
            Case Else
                Return "[#]unknowncode[" + code.ToString + "],[" + routineName + "]"
        End Select
    End Function

#End Region

End Class
